/**
 * @author cmtlyt
 * @version v1.0
 */

package chapter02;

/*
 算数运算符
 + - + - * / % ++ -- +
*/

public class ArithmeticOperator {
 public static void main(String[] args) {
  // 除号 /
  int num1 = 12;
  int num2 = 5;
  int result1 = num1 / num2;
  System.out.println(result1); // 2
  int result2 = num1 / num2 * num2;
  System.out.println(result2); // 10
  double result3 = num1 / num2;
  System.out.println(result3); // 2.0
  double result4 = num1 / num2 * num2;
  System.out.println(result4); // 12.0
  double result5 = (double) num1 / num2;
  System.out.println(result5); // 2.4
  double result6 = num1 * 1.0 / num2;
  System.out.println(result6); // 2.4

  // 取余 %
  // 结果的符合和被取余的数符号一样
  int m1 = 12;
  int n1 = 5;
  System.out.printf("%d % %d = %d", m1, n1, m1 % n1); // 2
  m1 = -12;
  n1 = 5;
  System.out.printf("%d % %d = %d", m1, n1, m1 % n1); // -2
  m1 = 12;
  n1 = -5;
  System.out.printf("%d % %d = %d", m1, n1, m1 % n1); // 2
  m1 = -12;
  n1 = -5;
  System.out.printf("%d % %d = %d", m1, n1, m1 % n1); // -2

  // ++
  int a1 = 10;
  int b1 = ++a1;
  System.out.printf("a1 = %d, b1 = %d", a1, b1);
  int a2 = 10;
  int b2 = a2++;
  System.out.printf("a2 = %d, b2 = %d", a2, b2);

  // 注意
  short s1 = 10;
  // 编译失败,所有非同类型的整数运算完之后的结果都是int
  // s1 = s1++;
  s1 = (short) s1++;
  System.out.println(s1);
  // 但是自增一不赋值的时候不会改变本身的数据类型
  s1++;
  System.out.println(s1);

  byte bb1 = 127;
  bb1++;
  System.out.println(bb1); // -128

  // --
  int a3 = 10;
  int b3 = ++a3;
  System.out.printf("a3 = %d, b3 = %d", a3, b3);
  int a4 = 10;
  int b4 = a4++;
  System.out.printf("a4 = %d, b4 = %d", a4, b4);
 }
}
